package com.tsu.mall.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tsu.mall.common.VO.*;
import com.tsu.mall.common.entity.Attachment;
import com.tsu.mall.common.entity.Wares;
import com.tsu.mall.common.entity.Waresclassify;
import com.tsu.mall.common.mapper.WaresMapper;
import com.tsu.mall.common.service.IWaresService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsu.mall.common.util.StringDbutil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tttttt
 * @since 2023-03-17
 */
@Service
public class WaresServiceImpl extends ServiceImpl<WaresMapper, Wares> implements IWaresService {

    @Override
    public LayuiTableVO doList(Integer page, Integer limit, String keyword,String waresStatus) {
        PageHelper.startPage(page,limit);
        if(waresStatus.equals("1")){
            waresStatus="已上架";
        }else if (waresStatus.equals("2")){
            waresStatus="未上架";
        }else{
            waresStatus="已下架";
        }
        List<Wares> list=baseMapper.doSelectListPage(keyword,waresStatus);
        LayuiTableVO vo = new LayuiTableVO();
        vo.setData(list);
        vo.setCount(new PageInfo<>(list).getTotal());
        return vo;
    }

    @Override
    public List<Waresclassify> selectChildren(Integer id) {
        List<Waresclassify> list=baseMapper.selectChildren(id);
        return list;
    }

    @Override
    public LayuiTableVO selectById(Integer id) {
        LayuiTableVO vo=new LayuiTableVO();
        Wares wares=baseMapper.selectById(id);
        //根据商品id查询对应的分类id对应的parent_id 即一级分类的id
        wares.setTopWares(baseMapper.selectWaresParent_id(id));
        wares.setSecondSelect(wares.getWaresclassifyId());
        vo.setData(wares);
        return vo;
    }

    @Override
    public ResultVO doSave(Wares wares) {
        ResultVO vo=new ResultVO();
        if(wares.getId()==null){
            if(!StringDbutil.isNoBlank(wares.getWaresName())) {
                vo.setCode(1);
                vo.setMsg("商品名不能为空！");
                return vo;
            }
            List<Wares> list=baseMapper.check(wares.getWaresName());
            if(list.size()>0) {
                vo.setCode(1);
                vo.setMsg("已存在该商品！");
                return vo;
            }
            wares.setWaresclassifyId(wares.getSecondSelect());//设置商品表的商品分类ID
            wares.setStatus("未上架");
            baseMapper.insert(wares);
        }else {//编辑管理员
            wares.setId(wares.getId());
            baseMapper.updateById(wares);
        }
        return vo;
    }

    @Override
    public void deleteWares(Integer[] ids) {
        baseMapper.deleteWares(ids);
    }

    @Override
    public void upShelfWares(Integer[] ids) {
        baseMapper.upShelfWares(ids);
    }

    @Override
    public void downShelfWares(Integer[] ids) {
        baseMapper.downShelfWares(ids);
    }

    @Override
    public void uploadAttachmentByIds(String ids, Integer id) {
        String[] str=ids.split(",");
        for (String s:str){
            baseMapper.uploadAttachmentByIds(Integer.parseInt(s),id);
        }
    }

    @Override
    public List<Attachment> selectList(Integer pid, String source) {
        return baseMapper.doSelectList(pid, source);
    }

    @Override
    public List<Attachment> selectByids(String ids) {
        String[] str=ids.split(",");
           return  baseMapper.selectByids(str);
    }

    @Override
    public List<WaresClassifyVO> selectClassifyList() {
        List<WaresClassifyVO> list=baseMapper.selectClassifyList();
        list.get(0).setTopIcon("fas fa-mobile-alt");
        list.get(1).setTopIcon("fas fa-laptop");
        list.get(2).setTopIcon("fas fa-tv");
        list.get(3).setTopIcon("fas fa-headphones-alt");
        list.get(4).setTopIcon("far fa-clock");
        list.get(5).setTopIcon("far fa-keyboard");
        list.get(6).setTopIcon("fas fa-universal-access");
        list.get(7).setTopIcon("fas fa-tshirt");
        return list;
    }

    @Override
    public List<Wares> selectBySecondId(Integer id) {
        return baseMapper.selectBySecondId(id);
    }

    @Override
    public Wares doSelectById(Integer id) {
        Wares waresInfo=baseMapper.selectById(id);
        return waresInfo;
    }

    @Override
    public List<Wares> selectCheapWares() {
        List<Wares> list=new ArrayList<>();
        //获取每个一级分类对应的所有二级分类中最新的哪一个分类
        List<Waresclassify> waresClassify=baseMapper.doSelectClassifyList();
        for (Waresclassify waresclassify:waresClassify){
            //获取当前分类中最便宜的一个
            Wares cheapWares=baseMapper.cheap(waresclassify.getId());
            if (cheapWares != null) {
                list.add(cheapWares);
            }
        }
        return list;
    }

    @Override
    public List<BillWaresVO> selectWaresByBillId(Integer billId) {
        return baseMapper.selectWaresByBillId(billId);
    }

    //查询销量前五的商品的信息
    @Override
    public List<Wares> selectSellTop5Wares() {
        return baseMapper.selectSellTop5Wares();
    }

    @Override
    public List<Wares> selectSellTop6to10WaresInfo() {
        return baseMapper.selectSellTop6to10WaresInfo();
    }

    @Override
    public List<IndexTableBingVO> dateBing() {
        return baseMapper.dateBing();
    }

}
